<template>
	<uni-popup ref="picPopRef" background-color="#fff" type="center" border-radius="10px 10px 0 0"
		:is-mask-click="false">
		<view class="popue">
			<view class="main">
				<picEditItem :item="item" :showClassid="true" @update:item="itemChange"></picEditItem>
			</view>
			<view class="popueBtnGroup">
				<button size="mini" type="primary" @click="submit">确认</button>
				<button size="mini" type="default" @click="close">取消</button>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
	import { ref } from 'vue';
	import picEditItem from './picEditItem.vue';
	import { showToast, uploadFileItem } from '../../../utils/common';
	import { cloudToHttps } from '../../../utils/tools';

	const props = defineProps(["item"]);
	const emits = defineEmits(["update:item", "success"]);
	const picPopRef = ref(null);
	const picCloudObj = uniCloud.importObject("admin-bizhi-picture");
	const imageCloudObj = uniCloud.importObject("handle-image");

	const open = () => {
		picPopRef.value.open();
	}

	const close = () => {
		picPopRef.value.close();
	}

	const itemChange = (e) => {
		emits("update:item", e);
	}

	const submit = async () => {
		let picurl = props.item.picurl;
		let file;
		if (props.item.tempurl && props.item.tempurl != props.item.picurl) {
			console.log("开始上传图片");
			file = await uploadFileItem(props.item.tempurl);
			picurl = cloudToHttps(file.fileID)
		}

		let { tempurl, ...params } = props.item;
		let { errCode, errMsg } = await picCloudObj.update({ ...params, picurl })
		if (errCode !== 0) showToast({ title: errMsg });

		console.log(props.item.picurl);
		console.log("上传完成的图片", file);
		if (file) imageCloudObj.remove(props.item.picurl)

		showToast({ title: "修改成功" })
		close();
		emits("success");
	}

	defineExpose({
		open,
		close
	});
</script>


<style lang="scss" scoped>
	.popue {
		width: 600px;
		min-height: 300px;
		background-color: #fff;
		padding: 50px;
	}
</style>